package com.yuzhitong.shapi.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串相关工具
 */
public class StringUtil {
    /**
     * 是否为中国手机号
     *
     * @param phone 需要判断文字
     * @return 判断结果
     */
    public static boolean isPhone(String phone) {
        String regex = "^(1[3-9])\\d{9}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            return m.matches();
        }
    }

    /**
     * 判断是否为网址
     *
     * @param text 需要判断文字
     * @return 判断结果
     */
    public static boolean isUrl(String text) {
        String regex = "^(http(s)?://)?([A-Za-z0-9]+\\.[A-Za-z0-9]+)+[\\S]*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);
        return m.matches();
    }

    /**
     * 判断是否为ip网址
     *
     * @param text 需要判断文字
     * @return 判断结果
     */
    public static boolean isIp(String text) {
        String regex = "^(http(s)?://)?[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+:[0-9]+[\\S]*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);
        return m.matches();
    }

    /**
     * 字符串是否为空
     */
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty() || str.trim().isEmpty();
    }


    /**
     * 将毫秒转时分秒
     *
     * @param time 毫秒值
     * @return 时分秒
     */
    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        if (hours > 0) {
            return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
        }
        if (minutes > 0) {
            return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
        }

        return String.format(Locale.getDefault(), "00:%02d", seconds);
    }

    /**
     * 将毫秒转时间日期时分秒
     *
     * @param time 毫秒值
     * @return 时分秒
     */
    public static String generateDataOrTime(long time) {
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        return format2.format(new Date(time));
    }
}
